import time

from selenium import webdriver

from Lib.zcm.exception import TestException
from ..common.ui.Selector import Selector
from .Button import Button
from .SwitchButton import SwitchButton

import pytest

def test_getValue():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/switchbutton/index.html")
    time.sleep(2)
    try:
        Button(browser, 'btn4', Selector.ID).click()
        time.sleep(2)
        switchbutton = SwitchButton(browser, 'switch', Selector.ID)
        assert True == switchbutton.getValue()
    finally:
        browser.close()

def test_set_value():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/switchbutton/index.html")
    time.sleep(2)
    try:
        switchbutton = SwitchButton(browser, 'switch', Selector.ID)
        switchbutton.setValue('on')
        assert True == switchbutton.getValue()
    finally:
        browser.close()


def test_set_value_change():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/switchbutton/index.html")
    time.sleep(2)
    try:
        switchbutton = SwitchButton(browser, 'switch', Selector.ID)
        current_value = switchbutton.getValue()
        switchbutton.setValue()
        after_set_value = switchbutton.getValue()
        assert current_value == bool(1-after_set_value)
    finally:
        browser.close()


def test_set_value_error():
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://fish.iwhalecloud.com/fish-desktop/examples/switchbutton/index.html")
    time.sleep(2)
    try:
        switchbutton = SwitchButton(browser, 'switch', Selector.ID)
        with pytest.raises(TestException):
            switchbutton.setValue('onxxxxxx')

    finally:
        browser.close()

if __name__ == '__main__':
    pytest.main(['-s','SwitchButton_test.py'])